using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using logistical.Models;
using common;

namespace logistical.Controllers
{
    public class order_mainController : baseController
    {
        public class list_order_main
        {
            public List<order_main> objs { get; set; }
        }

        [HttpGet]
        public object get()
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.order_main;
                var companyid1 = o.base_company.Where(x => x.companytype.Contains("1")).Select(x => new
                {
                    id = x.id,
                    text = x.name
                });
                UserInfo cuser = CurUser.get();
                if (companyid1.Where(x => x.id == cuser.account).Count() == 1)
                {
                    companyid1 = companyid1.Where(x => x.id == cuser.account);
                }
                var companyid2 = o.base_company.Where(x => x.companytype.Contains("3")).Select(x => new
                {
                    id = x.id,
                    text = x.name
                });
                var type = o.sys_iflag.Where(x => x.typecode == "ordertype1").Select(x => new
                {
                    id = x.code,
                    text = x.name
                }).OrderBy(x => x.id);
                var type2 = o.sys_iflag.Where(x => x.typecode == "ordertype2").Select(x => new
                {
                    id = x.code,
                    text = x.name
                }).OrderBy(x => x.id);
                var barea = o.base_area.Select(x => new
                {
                    id = x.id,
                    text = x.name
                });
                var earea = o.base_area.Select(x => new
                {
                    id = x.id,
                    text = x.name
                });
                var iflag = o.sys_iflag.Where(x => x.typecode == "ordertype3").Select(x => new
                {
                    id = x.code,
                    text = x.name
                }).OrderBy(x => x.id);

                return new
                {
                    data = p
                    ,
                    companyid1,
                    companyid2,
                    type,
                    type2,
                    barea,
                    earea,
                    iflag
                };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }


        [HttpPost]
        public object add(order_main obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                obj.id = Guid.NewGuid().ToString();
                common.sysEval.evalCurUserAndDate(obj);
                o.order_main.Add(obj);
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object edit(order_main obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.order_main.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.order_main.SingleOrDefault(x => x.id == obj.id);
                    old.code = obj.code;
                    old.companyid1 = obj.companyid1;
                    old.companyid2 = obj.companyid2;
                    old.contact = obj.contact;
                    old.mobile = obj.mobile;
                    old.remobile = obj.remobile;
                    old.company2 = obj.company2;
                    old.address = obj.address;
                    old.arrivedate = obj.arrivedate;
                    old.type = obj.type;
                    old.name = obj.name;
                    old.type2 = obj.type2;
                    old.weight = obj.weight;
                    old.volume = obj.volume;
                    old.barea = obj.barea;
                    old.earea = obj.earea;
                    old.tolvalue = obj.tolvalue;
                    old.iflag = obj.iflag;
                    old.filler = obj.filler;
                    old.filldate = obj.filldate;
                    old.remark = obj.remark;
                    common.sysEval.evalCurUserAndDate(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object del(order_main obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.order_main.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.order_main.SingleOrDefault(x => x.id == obj.id);
                    o.order_main.Remove(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object mdel(list_order_main objs)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                foreach (var p in objs.objs)
                {
                    var co = o.order_main.Where(x => x.id == p.id);
                    if (co.Count() == 1)
                    {
                        var old = o.order_main.SingleOrDefault(x => x.id == p.id);
                        o.order_main.Remove(old);
                    }
                }
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object wlth(order_main obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.order_main.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.order_main.SingleOrDefault(x => x.id == obj.id);
                    old.iflag = "3";
                    common.sysEval.evalCurUserAndDate(obj);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object thfth(order_main obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.order_main.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.order_main.SingleOrDefault(x => x.id == obj.id);
                    old.iflag = "5";
                    common.sysEval.evalCurUserAndDate(obj);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }
    }
}
